// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Load/SLOADParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/ISLOADService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (SLOAD);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/SLOAD/SLOAD.md>
*/
// 以下需要修改

SG::DataStructure::Common::Status BDF::BULKDATA::ParseSLOAD (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    // 定义两个 SLOADData 类型的临时对象，用于存储解析出的单点约束数据
    SG::DataStructure::FEM::SLOADData tempData;

    // TODO(bug 001) m_dominId的正确填写
    tempData.m_domainId = 1;
    tempData.m_sId      = convertToI64 (dividedEntryInfo[1]);
    // 初始化当前字段的索引值
    for (size_t i = 2; i < 9; i++)
    {
        // 检查当前字段是否为空白，如果为空则跳过
        bool const isEmpty = is_all_space (dividedEntryInfo[i]);
        if (isEmpty)
        {
            i++;
            continue;
        }
        tempData.m_G = convertToI64 (dividedEntryInfo[i]);
        tempData.m_F = convertToReal (dividedEntryInfo[++i]);
        if (dbServiceFactory->get<ISLOADService> ()->Add (tempData) == SG::DataStructure::Common::Status::STATUS_Failure)
        {
            throw std::runtime_error ("ISLOADService  Add Failure");
        }
    }

    // 将第一个节点的约束数据添加到数据库
    return SG::DataStructure::Common::Status::STATUS_Success;
}